package com.woniu.user.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 这个是消费者的配置
 */
@Configuration
public class ConsumerConfiguration {
     //注册交换机
    @Bean
    public TopicExchange userexchange(){
        return new TopicExchange("userexchange");
    }

    //注册topic类的死信交换机
    @Bean
    public TopicExchange userdelexchange(){
        return new TopicExchange("userdelexchange");
    }

    //注册队列
    @Bean
    public Queue userqueue(){
        Map<String, Object> arguments=new HashMap<>();
//        arguments.put("x-message-ttl",1000*60*60*24*2);
        arguments.put("x-message-ttl",1000*60*30);
        arguments.put("x-dead-letter-exchange","userdelexchange");
        arguments.put("x-dead-letter-routing-key","userdel");
        return new Queue("userqueue",true,false,false,arguments);
    }

    //注册死信队列
    @Bean
    public Queue userdelqueue(){
        return new Queue("userdelqueue");
    }

    //队列绑定交换机
    @Bean
    public Binding bindingUser(Queue userqueue,TopicExchange userexchange){
        return BindingBuilder.bind(userqueue).to(userexchange).with("#.user.#");
    }

    //死信队列绑定到交换机
    @Bean
    public Binding bindingUserDel(Queue userdelqueue,TopicExchange userdelexchange){
        return BindingBuilder.bind(userdelqueue).to(userdelexchange).with("#.userdel.#");
    }


}
